package com.sicheng.lc.杂题.括号系列;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/14 22:09
 */
public class 使括号有效的最少添加 {
    //https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/
    public int minAddToMakeValid(String s) {
        int l = 0, r = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(')
                l++;
            if (s.charAt(i) == ')') {
                if (l > 0) {
                    r++;
                    l--;
                }
            }
        }
        return s.length() - r * 2;
    }
}
